/*
 TwitterFeedController.m
 LegislatorLookup
 
 This is the controller for the Twitter List. This page display's a 
 candidate's twitter feed
 
 Copyright (c) 2009 Nabil Ahmad
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 
 1. Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.
 
 2. Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer in the 
 documentation and/or other materials provided with the distribution. 
 
 3. The name of the author may not be used to endorse or promote products
 derived from this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */	

#import "TwitterFeedViewController.h"
#import "WebServiceController.h"

@implementation TwitterFeedViewController
// Custom init method to take twitter User name
- (id) initWithTwitterId:(NSString *)twitter {
	[super initWithStyle:UITableViewStyleGrouped];
	twitterId = twitter;
	
	return self;
}

- (void)loadView {
	[super loadView];
	self.title = @"Twitter Feed";
	isDataLoaded = NO;
}

// load data and rerender view after view is loaded
- (void)viewDidAppear:(BOOL)animated
{
	// load data
	feed = [[WebServiceController getTimelineForUsername:twitterId] retain];
	isDataLoaded = YES;
	[self.tableView reloadData];
}


// Return number of tweets
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	int count = 0;
	
	if (feed != nil && isDataLoaded) 		
		count = feed.count;

	return count;
}

// Return cell with two sub rows (allowing for created timestamp)
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

	UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];	
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	
	UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(5.0, 5.0, 295, 16)] autorelease];

	label.font = [UIFont boldSystemFontOfSize:12.0];
	label.textAlignment = UITextAlignmentLeft;
	label.textColor = [UIColor grayColor];
	label.text = [[feed objectAtIndex:indexPath.row] objectForKey:@"created_at"];
	[cell.contentView addSubview:label];
	
	
	UILabel *value = [[[UILabel alloc] initWithFrame:CGRectMake(5.0, 24, 295, cell.bounds.size.height - 30)] autorelease];
	value.font = [UIFont boldSystemFontOfSize:12.0];
	value.textColor = [UIColor blackColor];
	value.textAlignment = UITextAlignmentLeft;
	value.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
	[cell.contentView addSubview:value];
	value.numberOfLines = 0;
	value.text = [[feed objectAtIndex:indexPath.row] objectForKey:@"text"];
	
	return [cell autorelease];
}

// dynamically determine hight based on amount of text + font + size
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	NSString *text = [[feed objectAtIndex:indexPath.row] objectForKey:@"text"];
	UIFont *font = [UIFont boldSystemFontOfSize:14];
	
	CGSize withinSize = CGSizeMake(320 , 1000);
	CGSize size = [text sizeWithFont:font constrainedToSize:withinSize lineBreakMode:UILineBreakModeWordWrap];
	
	return size.height + 30;		
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (void)dealloc {
    [super dealloc];
	[feed release];
}


@end

